<?php

// 修改我的信息
// PUT member.upUinfo

namespace app\api\resource\member;
use think\Db;
use think\facade\Session;
use app\common\validate\Member as MemberValidate;

class UpUinfo
{
    // 方法
    public $_method = ['PUT','OPTIONS'];
    // 前置
    public $_pre    = ['member'];
    // 描述
    public $_description = '编辑我的信息';
    // 参数
    public $_param  = [
        'nickname'   => '昵称,可选',
        'headimgurl' => '头像,可选',
        'ext'        => '扩展字段,可选',
    ];

    public function run(&$request)
    {
        $params   = $request->param();
        $validate = new MemberValidate;
        if ( !$validate->scene('upUinfo')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }
		
		if ( empty($params) ) return [422,'参数必须'];

        $mid = Session::get('member.id');

        if ( isset($params['nickname']) )   $data['nickname']   = $params['nickname'];
        if ( isset($params['headimgurl']) ) $data['headimgurl']   = $params['headimgurl'];
        if ( isset($params['ext']) )        $data['ext']   = $params['ext'];
        $num = Db::name('member')->where('id',$mid)->update($data);

        return ($num >= 0) ? [201,'修改成功'] : 500;
    }
}
